/**
 * Copyright (C) 2016-2019 DSpot Sp. z o.o
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dspot.declex.actions;

import ohos.agp.components.Component;
import ohos.agp.components.TimePicker;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.Calendar;

import org.ohosannotations.annotations.RootContext;


import com.dspot.declex.actions.component.TimeDialog;
import com.dspot.declex.annotation.action.ActionFor;
import com.dspot.declex.annotation.action.Assignable;
import com.dspot.declex.annotation.action.FormattedExpression;
import com.dspot.declex.annotation.action.StopOn;

/**
 * An Action representing a dialog that prompts the user for the time of day using a
 * {@link TimePicker}.
 *
 * <p>
 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
 * guide.
 */

@ActionFor("TimeDialog")
public class TimeDialogActionHolder {

    TimeDialog dialog;

    @RootContext
    Context context;

    private TimeSetRunnable TimeSet;

    void init() {
        init(null);
    }

    /**
     * @param calendar A Calendar object containing the initial hour and minute
     * for the time picker.
     */
    void init(Calendar calendar) {
        if (calendar == null) {
            // Use the current time as the default values for the picker
            calendar = Calendar.getInstance();
        }

        final int hour = calendar.get(Calendar.HOUR);
        final int minute = calendar.get(Calendar.MINUTE);

        init(hour, minute);
    }

    /**
     * @param hour The initial hour of the dialog.
     * @param minute The initial minute of the dialog.
     */
    void init(int hour, int minute) {
        // Create a new instance of TimePickerDialog and return it
        dialog = new TimeDialog(context);
        dialog.setHour(hour);
        dialog.setMinute(minute);
        dialog.set24Hour(true);
        dialog.setTimeChangedListener(new TimePicker.TimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute, int second) {
                if (TimeSet != null) TimeSet.onTimeChanged(timePicker, hour, minute, second);
            }
        });
    }

    /**
     * @param TimeSet <i><b>(default)</b></i> This Action Selector will be executed when the Time is set.
     * <br><br>Parameters
     * <ul>
     * <li>datePicker - The DatePicker View associated with this listener.</li>
     * <li>hour - The hour that was set.</li>
     * <li>minute - The minute that was set.</li>
     * </ul>
     * @param Destroy This Action Selector will be invoked when the dialog is Destroy.
     */
    void build(
        final TimeSetRunnable TimeSet,
        final Runnable Destroy) {

        this.TimeSet = TimeSet;

        if (Destroy != null) {
            dialog.setDestroyedListener(new CommonDialog.DestroyedListener() {
                @Override
                public void onDestroy() {
                    Destroy.run();
                }
            });
        }
    }

    ;

    void execute() {
        dialog.show();
    }

    /**
     * @return Internal Android Dialog instance.
     */
    @StopOn("show")
    public TimeDialog dialog() {
        return this.dialog;
    }

    /**
     * Assigns the Internal Android Dialog instance.
     *
     * @param dialog The variable to which the dialog is going to be assigned
     */
    public TimeDialogActionHolder dialog(@Assignable("dialog") TimeDialog dialog) {
        return this;
    }

    public TimeDialogActionHolder title(@FormattedExpression String title) {
        dialog.setTitleText(title);
        return this;
    }

    public TimeDialogActionHolder message(@FormattedExpression String message) {
        dialog.setContentText(message);
        return this;
    }

    public static abstract class TimeSetRunnable implements Runnable, TimePicker.TimeChangedListener {

        public TimePicker timePicker;
        public int hour;
        public int minute;
        public int second;

        @Override
        public void onTimeChanged(TimePicker timePicker, int hour, int minute, int second) {
            this.timePicker = timePicker;
            this.hour = hour;
            this.minute = minute;
            this.second = second;
            run();
        }
    }
}
